import LuckyExcel from "luckyexcel";


var mixins={
    data(){
        return {
        oneK:{},
        allK:{},
        newK:{},
        allK2:{},
        newK2:{},
        thousandData:[],
        fnThousandData:[],
        }
    },
    methods:{
        // 接口文件流形式下载文件
        mixFileDownLoadFn(url,param,fileName,type){
            let preFn=function(){};
            preFn(url,param).then(res=>{
                let name=""
                try{
                    name=(decodeURI(res.headers["content-disposition"].split("=")[1].split(";")[0]||`${fileName}`))
                }catch(err){
                    name=`${fileName}`
                }
                const data=res.data
                let blob=new Blob([data],{type:"application/vnd.ms-excel;charset=utf-8"})
                let winUrl=window.URL.createObjectURL(new Blob([data]))
                if(window.navigator.msSaveBlob){
                    // 兼容ie浏览器下载
                    window.navigator.msSaveBlob(blob,name)
                }else{
                    let a=document.createElement("a")
                    a.style.display="none"
                    a.href=winUrl
                    a.setAttribute("download",name)
                    document.body.appendChild(a)
                    a.click()
                    document.body.removeChild(a)
                    return true
                }
            })
        },
        // 渲染luckysheet函数
        async exportLuckySheet(files,receiveData){
            await this.readFile(files,receiveData)
        },
        // 使用exceljs解读文件
        async readFile(file,receiveData){
            let rows=[]
            const Excel=require("exceljs")
            const reader=new  FileReader(file)
            reader.onload=async (e)=>{
                const data=new Uint8Array(e.target.result)
                let workbook=new Excel.Workbook()
                await workbook.xlsx.load(data)
                workbook.worksheets.map((item)=>{
                    let h=this.readSheetData(item)
                    rows.push(h)
                })

                // 检索出文件中所有的千分位数据
                let thousandData=[] //不包含函数的千分位数据
                let fnThousandData=[] //函数的千分位数据
                rows.map((items)=>{
                    let thousandDataOne=[]
                    let fnThousandDataOne=[]
                    items.map(item=>{
                        if(item.style.numFmt&&item.style.numFmt.indexOf("#,##0.")!=-1&&item.value&&!item.value.formula){
                            thousandDataOne.push(item)
                        }
                        if(item.style.numFmt&&item.style.numFmt.indexOf("#,##0.")!=-1&&item.value&&item.value.formula){
                            fnThousandDataOne.push(item)
                        }
                    })
                    thousandData.push(thousandDataOne)
                    fnThousandData.push(fnThousandDataOne)
                })
                this.thousandData=thousandData
                this.fnThousandData=fnThousandData
                console.log("查看最终检索出来的数据",thousandData,fnThousandData)
                this.exportLuckySheet2(file,receiveData)
            }
            reader.readAsArrayBuffer(file)

        },
        // 解析excel文件内容样式
        readSheetData(worksheets){
            let rows=[]
            worksheets.eachRow((row,rowNumber)=>{
                const rowData=[]
                row.eachCell((cell,colNumber)=>{
                    const cellValue=cell.value;
                    const celStyle=cell.style;
                    rowData.push({value:cellValue,style:celStyle,row:rowNumber,col:colNumber})
                })
                rows=rows.concat(rowData)
            })
            return rows

        },
        // luckysheet渲染前数据处理
        async exportLuckySheet2(files,receiveData){
            LuckyExcel.transformExcelToLucky(files,(exportJson)=>{
                console.log("查看解析完成初始数据=========",exportJson)
                let exportJson1=""
                try{
                    exportJson1=this.di(exportJson)
                    exportJson1.sheets.map(item=>{
                        item.celldata.map(i2=>{
                            if(typeof i2.v=="object"&&i2.v.ct=='object'){
                                if(i2.v.ct.s&&typeof i2.v.ct.s=="object"&&i2.v.ct.s[0]){
                                    let v=""
                                    for(let i in i2.v.ct.s[i2.v.ct.s.length-1]){
                                        if(i!="v"){
                                            i2.v[i]=i2.v.ct.s[i2.v.ct.s.length-1][i]
                                        }
                                    }

                                    if(i2.v.ct.s[0]){
                                        i2.v.ct.s.map(item=>{
                                            v+=item.v
                                        })
                                        i2.v.ct.s=""
                                    }

                                    if(v){
                                        i2.v.v=v
                                        // 如果是函数则不处理格式
                                        if(!i2.v.f){
                                            this.dataProcessing(v,i2)
                                        }
                                    }
                                }else{
                                    let a=i2.v.v
                                    if(!i2.v.f){
                                        this.dataProcessing(a,i2)
                                    }
                                }
                            }else{
                                let a=i2.v.v
                                if(!i2.v.f){
                                    this.dataProcessing(a,i2)
                                }
                            }
                        })
                    })
                    console.log("查看递归完成数据============",exportJson1)

                }catch(err){
                    console.log("查看报错信息",err)
                }
                if(receiveData){
                    this.options2.data=exportJson1.sheets
                    this.options2.title=exportJson1.info.name
                    // window.Luckysheet.destroy()
                    this.creatLuckySheetContainer(receiveData)
                }else{
                    this.options.data=exportJson1.sheets
                    this.options.title=exportJson1.info.name
                    // window.Luckysheet.destroy()
                    this.creatLuckySheetContainer()
                }

            })
        },
        

        // 千分位格式数据处理
        dataProcessing(a,i2){
            if(i2.v&&i2.v.ct&&i2.v.ct.fa.indexOf("#,##0")!=-1){
                // 当前已经是千分位格式的数据，跳出处理
                return
            }

            if(a){
                // 判断是一个正确的负数
                if(a.indexOf("-")!="-1"){
                    if(a[0]!="-"||a.replace(/[^-]/g,"").length>1){
                        //不是一个正确的负数，跳出
                        return
                    }
                }

                if(a.replace(/[^.]/g,"").length>1){
                    //不是一个正确的小数，跳出
                    return
                }

                if(!a.replace(/[0-9,.-]/g,"")){
                    // 判断无指定字符之外的字符
                    let c=a.replace(/[,]/g,"")
                    if(c.length>15){
                        // 如果位数大于15位处理
                        i2.v.ct={fa:"@",t:"g"}
                        i2.v.v=String(c)
                    }else{
                        if(c.indexOf(".")!=-1){
                            let f=c.split(".")[1]
                            let d=f.replace(/[0-9]/g,"")
                            if(!d){
                                let e="."
                                for(let i in f){
                                    e+="0"
                                }
                                let fa="#,##0"+e
                                i2.v.ct={fa,t:"n"}
                                i2.v.v=Number(c)
                            }
                        }else{
                            i2.v.ct={fa:"#,##0",t:"n"}
                            i2.v.v=Number(c)
                        }
                    }
                }

            }
        },
        // luckysheet数据递归处理
        di(item){
            let kk;
            // 判断出具体数据类型，创建初始数据对象
            if(item instanceof Array){
                if(item==null){
                    kk=null
                }else{
                    kk=[]
                }
            }else{
                if(item==null){
                    kk=null
                }else{
                    kk={}
                }
            }
            for(let index in item){
                if(typeof item[index] == "object"){
                    if(index=="ct"&&!item.[index].fa&&!item[index].t){
                        item[index]={fa:"yyy-MM-dd",t:"d"}
                    }
                    kk[index]=this.di(item[index])
                }else{
                    if(index=="f"){
                        let a=item[index]
                        let aIndex=item[index].indexOf("==")
                        if(aIndex===0){
                            let cd=item[index].substr(1,item[index].length)
                            kk.f=cd
                        }else{
                            kk.f=item[index]
                        }
                    }else if(index=="bg"){
                        kk[index]=this.rgb2hex(item[index],"#ffffff")
                    }
                    else if(index=="fc"){
                        kk[index]=this.rgb2hex(item[index],"#000000")
                    }
                    else if((index=="fa"&&item[index]=='#,##0_);[Red]\\(#,##0\\)')||(index=="fa"&&item[index]=='#,##0_);[Red](#,##0)')){
                        kk[index]='#,##0_);[Red](#,##0)'
                        kk.t="n"
                    }
                    else{
                        kk[index]=item[index]
                    }
                }
            }
            return kk
        },

        rgb2hex(srgb,color){
            var reg=new RegExp(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/)
            let res=reg.test(srgb)
            if(!res){
                if(srgb.indexOf(",")==-1){
                    return srgb
                }else{
                    return color
                }
            }else{
                var str="#"
                let ret=srgb.match(reg)
                for(var i=1;i<4;i++){
                    var m=(ret[i]);
                    str+=(m<16?"0"+m.toString(16):m.toString(16))
                }
                return str
            }
        },

    }
}

export default mixins